// pages/lsittest/listtest.js
const app = getApp()
let leftHeight = 0,
  rightHeight = 0; //分别定义左右两边的高度
let query;
Page({
  data: {
    list: [],
    leftList: [],
    rightList: [],
    sslist:[],
    showSearch: false,
    inputFocus: false,
    isshow:true,
    
  },
  gotoadd(){
    wx.navigateTo({
      url: '../add/add?id='+"1",
    });
  },
  gotoArticleDetail: function (event) {
    // 当前要跳转到另一个界面，但是会保留现有界面
    console.log("点击"+event.currentTarget.dataset.articleid);
    wx.navigateTo({
      url: '../text/text?id='+event.currentTarget.dataset.articleid,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.request({
      url:app.globalData.url+"/wordlist",
      method:"GET",
      success: res => {
        console.log(res);
        this.setData({list:res.data.content})
        this.isLeft();
      },
      fail: err => {
        console.log("请求失败",err);
        wx.showToast({
          title: '请求失败',
          icon: 'error',
          duration: 2000
        })
      }
    })
    
  },
  onFocus: function() {
    this.setData({
      inputFocus: true,
      showSearch: true,
      isshow:false
    });
  },
  onBlur: function(e) {
    this.setData({
      isshow:false,
      showSearch: true,
      searchResult:null,
      Searchtxt:e.detail.value
    })
    var txt=this.data.Searchtxt;
    var dataArray=this.data.list
    console.log("开始搜索"+txt);
    const searchResult = this.fuzzySearch(dataArray, txt);
    console.log(searchResult);
    this.setData({
      searchResult:searchResult
    })
  },
  fuzzySearch(arr, searchText) {
    const filteredArray = arr.filter(item => {
      const titleMatch = item.title.toLowerCase().includes(searchText.toLowerCase());
      const timeMatch = item.releasetime.toLowerCase().includes(searchText.toLowerCase());
      const textMatch = item.text && item.text.toLowerCase().includes(searchText.toLowerCase());
      return titleMatch || timeMatch || textMatch;
    });
    return filteredArray;
  },
  onArticleTap:function(event){
    wx.navigateTo({
      url: '../text/text?id='+event.currentTarget.dataset.articleid,
    });
  },
  quxiao: function() {
    this.setData({
      isshow:true,
      showSearch: false,
    })
  },

  isLeft() {
    const {
      list,
      leftList,
      rightList
    } = this.data;
    query = wx.createSelectorQuery();
    this.loopList(0)
  },

  loopList(index){
    const {
      list,
      leftList,
      rightList
    } = this.data;
    if (!list[index]) return;
    leftHeight <= rightHeight ? leftList.push(list[index]) : rightList.push(list[index]); //判断两边高度，来决定添加到那边
    this.getBoxHeight(leftList, rightList).then(()=>{
      this.loopList(++index)
    })
  },
  getBoxHeight(leftList, rightList) { //获取左右两边高度
    return new Promise((resolve, reject) => {
      this.setData({
        leftList,
        rightList
      }, () => {
        query.select('#left').boundingClientRect();
        query.select('#right').boundingClientRect();
        query.exec((res) => {
          leftHeight = res[0].height; //获取左边列表的高度
          rightHeight = res[1].height; //获取右边列表的高度
          resolve();
        });
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function() {
  //     var dataList = this.data.list;
  //   var index = this.data.list.length;
  //     this.setData({
  //       list: this.data.list.concat(dataList)
  //     })
  //     console.log("--------",this.data.list)
  //   this.loopList(index)
  // },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
